package kr.ac.kaist.idb.csnql.clause;

import java.io.Serializable;
import java.util.Vector;

/**
 * 
 * @brief WHEN clause class
 * @author Jongheon Park (korex527@kaist.ac.kr)
 *
 */
public class SNQLWhenExp implements SNQLExp, Serializable
{
	/**
	 * @brief Serial version UID which is automatically generated
	 */
	private static final long serialVersionUID = 4763331662336568750L;

	/** @brief Event expression */
	public SNQLEvent iexp;
	
	/** @brief EVENT type constant */
	public final static int WHEN_TYPE_EVENT = 11;
	
	/** @brief EXPRESSION type constant */
	public final static int WHEN_TYPE_EXPRESSION = 12;
	
	/** @brief The WHEN type. */
	public int whenType;
	
	/** @brief Constant THEN_TYPE_SELECT. */
	public final static int THEN_TYPE_SELECT = 1;
	
	/** @brief Constant THEN_TYPE_WHERE. */
	public final static int THEN_TYPE_WHERE = 2;
	
	/** @brief Constant THEN_TYPE_SELECT_WHERE. */
	public final static int THEN_TYPE_SELECT_WHERE = 3;
	
	/** @brief Constant THEN_TYPE_SAMPLE. */
	public final static int THEN_TYPE_SAMPLE = 4;
	
	/** @brief Constant THEN_TYPE_SELECT_SAMPLE. */
	public final static int THEN_TYPE_SELECT_SAMPLE = 5;
	
	/** @brief Constant THEN_TYPE_WHERE_SAMPLE. */
	public final static int THEN_TYPE_WHERE_SAMPLE = 6;
	
	/** @brief Constant THEN_TYPE_QUERY. */
	public final static int THEN_TYPE_QUERY = 7;
	
	/** @brief The THEN type. */
	public int thenType;
	
	/** @brief Changable SELECT clause */
	public Vector<Object> select_;
	
	/** @brief Changable SAMPLE PERIOD-FOR clause */
	public SNQLSample sample_;
	
	/** @brief Changable WHERE clause */
	public SNQLExpression where_;
	
	/**
	 * @brief Instantiates a new SNQLWhenExp
	 */
	public SNQLWhenExp()
	{
		select_ = new Vector<Object>();
		iexp = new SNQLEvent();
		sample_ = new SNQLSample();
		where_ = new SNQLExpression();
	}
	
	
	/**
	 * @brief Insert select.
	 *
	 * @param si the selectItem list
	 */
	@SuppressWarnings("unchecked")
	public void insertSelect(Vector<Object> si)
	{
			select_ = (Vector<Object>) si.clone();
	}
	
	/**
	 * @brief Insert where.
	 *
	 * @param wh the WHERE clause
	 */
	public void insertWhere(SNQLExp wh)
	{
		where_ = (SNQLExpression) wh;
	}
	
	/**
	 * @brief Insert sample.
	 *
	 * @param sm the SAMPLE PERIOD-FOR clause
	 */
	public void insertSample(SNQLSample sm)
	{
		sample_ = sm;
	}
	
	
	
	/**
	 *
	 * @return The WHEN type
	 * @brief Get the WHEN type
	 */
	public int getWhenType() {
		return whenType;
	}

	/**
	 * @param whenType New WHEN type
	 * @brief Set the WHEN type
	 */
	public void setWhenType(int whenType) {
		this.whenType = whenType;
	}

	/**
	 * @return The THEN type
	 * @brief Get the THEN type
	 */
	public int getThenType() {
		return thenType;
	}

	/**
	 * @param thenType New THEN type
	 * @brief Set the THEN type
	 */
	public void setThenType(int thenType) {
		this.thenType = thenType;
	}
	
	/**
	 * @return The event expression
	 * @brief Get the event expression
	 */
	public SNQLEvent getIexp() {
		return iexp;
	}

	/**
	 * @param iexp New event expression
	 * @brief Set the event expression
	 */
	public void setIexp(SNQLEvent iexp) {
		this.iexp = iexp;
	}

	/**
	 * @return The SELECT part of WHEN clause
	 * @brief Get the SELECT part of WHEN clause
	 */
	public Vector<Object> getSelect_() {
		return select_;
	}

	/**
	 * @param select_ New SELECT part of WHEN clause
	 * @brief Set the SELECT part of WHEN clause
	 */
	public void setSelect_(Vector<Object> select_) {
		this.select_ = select_;
	}

	/**
	 * @return The SAMPLE PERIOD-FOR part of WHEN clause
	 * @brief Get the SAMPLE PERIOD-FOR part of WHEN clause
	 */
	public SNQLSample getSample_() {
		return sample_;
	}

	/**
	 * @param sample_ New SAMPLE PERIOD-FOR part of WHEN clause
	 * @brief Set the SAMPLE PERIOD-FOR part of WHEN clause
	 */
	public void setSample_(SNQLSample sample_) {
		this.sample_ = sample_;
	}

	/**
	 * @return The WHERE part of WHEN clause
	 * @brief Get the WHERE part of WHEN clause
	 */
	public SNQLExpression getWhere_() {
		return where_;
	}

	/**
	 * @param where_ New WHERE part of WHEN clause
	 * @brief Set the WHERE part of WHEN clause
	 */
	public void setWhere_(SNQLExpression where_) {
		this.where_ = where_;
	}

	public String toString()
	{
		StringBuffer stringbuffer = new StringBuffer();
		
		stringbuffer.append(" WHEN " + iexp.toString() + "\n ");
		
		if(thenType ==  THEN_TYPE_SELECT || thenType == THEN_TYPE_SELECT_SAMPLE || thenType == THEN_TYPE_SELECT_WHERE || thenType == THEN_TYPE_QUERY)
		{
			stringbuffer.append(" SELECT ");
        	stringbuffer.append(select_.elementAt(0).toString());
        	for(int i = 1; i < select_.size(); i++)
                stringbuffer.append(", " + select_.elementAt(i).toString());
		}
		
		if(thenType ==  THEN_TYPE_WHERE || thenType == THEN_TYPE_WHERE_SAMPLE || thenType == THEN_TYPE_SELECT_WHERE || thenType == THEN_TYPE_QUERY)
            stringbuffer.append(" WHERE " + where_.toString());
		
		if(thenType ==  THEN_TYPE_SAMPLE || thenType == THEN_TYPE_WHERE_SAMPLE || thenType == THEN_TYPE_SELECT_SAMPLE || thenType == THEN_TYPE_QUERY)
        	stringbuffer.append(" "  + sample_.toString());
		//@todo
		return stringbuffer.toString();
	}
}
